// 凯撒大帝的加密方式： 字母偏移
let offset = 10
let word_arr = [..."QWERTYUIOPASDFGHJKLZXCVBNM"]

function encrypt(plan_text,offset=0){
    const plan_text_arr = [...plan_text]
    const encrypted_text = plan_text_arr.map(item=>{
        let index = word_arr.indexOf(item.toUpperCase())
        if (index<0){
            return item
        }
        if (index + offset < word_arr.length){
            index = index + offset
        }else {
            index = index + offset - word_arr.length
        }
        return word_arr[index]
    })
    return encrypted_text.join("")
}

function decrypt(plain_text,offset=0){
    const plan_text_arr = [...plain_text]
    const decrypted_text_arr = plan_text_arr.map(item=>{
        let index = word_arr.indexOf(item.toUpperCase())
        if (index<0){
            return item
        }
        if (index - offset < 0){
            index = word_arr.length  - index - offset
        }else {
            index = index - offset
        }
        return word_arr[index]
    })
    return decrypted_text_arr.join("")
}

let plain_text = "mMMMhello i am dujianjun";
console.log("plain_text:",plain_text);
let encrypted = encrypt(plain_text,1)
console.log("encrypted:", encrypted)
let decrypted = decrypt(encrypted,1)
console.log("decrypted:", decrypted)

// 按照一个算法打乱
function encrypt2(plain,fn){
    let word_arr = [...plain]
    let encrypted = word_arr.map((item,index)=>{
        let i = fn(index)
        for (;i > word_arr.length - 1; i = i - word_arr.length){}
        return word_arr[i]
    })
    return encrypted.join("")
}
fn = function (i) {
    return i*33 + 51
}
console.log(encrypt2("含有五个元音字母的最短的英语单词是sequoia（红杉）。\n" +
    "不含任何元音字母的最短英语单词是by、my。\n" +
    "根据统计，单词中使用最多的字母是e，平均每八个中有一个。\n" +
    "用英语从一数到一百，你会发现有一个元音字母从不出现，这个字母就是a。\n" +
    "有些单词与字母的发音完全相同，如：be(是)/bee(蜜",fn))

function decrypt2(text, fn){

}

fn2 = function (text,i) {
    if ((i/33+51)%10 !== 0){
        i += text.length
    }

}
